package shop.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import shop.dao.IProductDao;
import shop.model.Product;
import shop.service.IProductService;

@RestController
@RequestMapping("product")
public class ProductController {
	
	@Resource
	private IProductDao dao;

	@Resource
	private IProductService service;

	// 上传图片
	@RequestMapping("/upload.do")
	@ResponseBody
	public Map upload(MultipartFile file) {
		// 获取文件原始名
		String name = file.getOriginalFilename();
		// System.out.println(name);
		// String path = "d:\\upload\\" + name;//上传到本地磁盘
		String path = System.getProperty("evan.webapp") + "upload\\" + name;// 上传到tomcat服务器
		System.out.println(path);
		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", name);
		return map;
	}

	@RequestMapping("isPutOnSaleYes.do")
	public int isPutOnSaleYes(Integer id) {
		return service.isPutOnSaleYes(id);
	}
	
	//今日推荐商品
	@RequestMapping("findToday.do")
	public List<Product> findToday(){
		return service.findToday();
	}
	
	//id查询
	@RequestMapping("findById.do")
	public Product findById(Integer id) {
		return dao.findById(id);
	}

}
